<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <title>深度监视</title>
</head>

<body>
    <div id="root">
        <h2>今天天气很{{info}}</h2>
        <button @click="change">切换天气</button>
        <!-- 模板不使用methos的情况  @click="可以写简单JS语句"-->
        <!-- <button @click="Hot = !Hot">切换天气</button> -->
        <h1>a的值是{{number.a}}</h1>
        <button @click="number.a++">让a+1</button>

        <h1>b的值是{{number.b}}</h1>
        <button @click="number.b++">让b+1</button>
        <!-- 创造新的对象 -->
        <button @click="number = {a:666,b:888}">让number改变</button>
    </div>

    <script>
        const vm = new Vue({
            el: "#root",
            data: {
                Hot: true,
                number: {
                    a: 1,
                    b: 1,
                }
            },
            computed: {
                info() {
                    return this.Hot ? "炎热" : "凉爽"
                }
            },
            methods: {
                change() {
                    this.Hot = !this.Hot
                }
            },
            //    监视属性必须存在才能监视  可以监视属性和计算属性
            watch: {
                Hot: {
                    immediate: true,//初始化的时候调用handler
                    handler(newValue, oldValue) {
                        console.log("Hot被修改了", "更改后是" + newValue, "原来是" + oldValue)
                    }
                },
                info: {
                    immediate: true,//初始化的时候调用handler
                    handler(newValue, oldValue) {
                        console.log("info被修改了", "更改后是" + newValue, "原来是" + oldValue)
                    }
                },
                // a不是直接的属性，不存在于vm中 保存在number中
                // a:{
                //     handler(){
                //         console.log("a+1")
                //     }
                // }
                "number.a": {
                    handler() {
                        console.log("a+1")
                    }
                },
                "number.b": {
                    handler() {
                        console.log("b+1")
                    }
                },
                number: {
                    deep: true, //深度监视 默认为 false
                    handler() {
                        console.log("number的值有改变的了")
                    }
                }
            }
        })

    </script>

</body>

</html>